

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class _JsoupUtils {

	public static void main(String[] args) throws IOException {
		
		
		Connection con = Jsoup.connect("http://www.gsxt.gov.cn/corp-query-search-1.html");
		con.header("Upgrade-Insecure-Requests", "1");
		con.header("Content-Type", "application/x-www-form-urlencoded");
		con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0");
		
		Map<String,String> cookies = new HashMap<>();
		cookies.put("__jsluid", "2b9317f8dac8df4d27064fff9358639c");
		cookies.put("__jsl_clearance", "1545903924.471|0|txh2FJqI45ar5AW0VxbikjUH2Ps%3D");
		cookies.put("tlb_cookie", "S172.16.12.102");
		cookies.put("SECTOKEN", "7176069122535196668");
		cookies.put("JSESSIONID", "C79771F55BE91065E1CBAC918CFD5198-n1:7");
		cookies.put("gsxtBrowseHistory1",
				"%0FS%04%06%1D%04%1D%10SNS%24%26%3B%22%3D%3A71%"
						+ "3A%3B01%3A%219EEDDDDFD%11GL%16L%16AD%12D%12BADDEAD%12MLLE%12%15%16A%40M"
						+ "%40SXS%11%1A%00%1A%15%19%11SNS%E5%8D%A3%E4%BB%98%E6%84%93%E4%BB%A5%E5%89"
						+ "%AF%E6%82%87%E7%A6%A5%E6%8B%B4%E6%9D%BD%E9%98%A4%E5%84%98%E5%8E%8CSXS%11%"
						+ "1A%00%00%0D%04%11SNEEGDXS%02%1D%07%1D%00%00%1D%19%11SNEA%40AMD%40FLAGEF%09");
		con.cookies(cookies);
		
		con.data("tab", "ent_tab");
		con.data("province", "");
		con.data("tab", "ent_tab");
		con.data("geetest_challenge", "c150b87a2f376903f29ce5ee406eb59e");
		con.data("geetest_validate", "f8231511e61cb2e66b1a188a0c432965");
		con.data("geetest_seccode", "f8231511e61cb2e66b1a188a0c432965%7Cjordan");
		con.data("token", "88316230");
		con.data("searchword", "%E5%8C%97%E4%BA%AC%E4%BA%91%E5%A4%A9%E6%97%B6%E7%A9%BA");
		
		System.out.println(con.post());

	}

	public static void xwqyGsxtGovCn() throws IOException {
		Map param = new HashMap<String, String>();

		param.put("organId", "100000");
		param.put("textfield", URLEncoder.encode("玺德康"));
		param.put("fwId", "1400");
		param.put("searchOrganId", "");
		param.put("channelId", "99");
		param.put("captcha", "");
		param.put("geetest_challenge", "c16a5320fa475530d9583c34fd356ef5d0");
		param.put("geetest_validate", "ee55e44545e93_ee55eee44f92c_54e555e54ee5a3");
		param.put("geetest_seccode", "ee55e44545e93_ee55eee44f92c_54e555e54ee5a3|jordan");

		Connection connect = Jsoup.connect("http://xwqy.gsxt.gov.cn/mirco/micro_lib");
		connect.data(param);
		connect.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/64.0");
		Document post = connect.post();

		String htmlPage = post.toString();

		int startIndex = htmlPage.indexOf("var objTbody = ");
		int endIndex = htmlPage.indexOf("function tbodyList(array,start,len){");

		String jsonStr = htmlPage.substring(startIndex, endIndex - 3);
		JSONArray parseArray = JSON.parseArray(jsonStr.split("objTbody = ")[1]);

		for (Object object : parseArray) {
			String regno = ((JSONObject) object).getString("regno");
			String entname = ((JSONObject) object).getString("entname");
			System.out.println(regno + " " + entname);
		}

		//_HttpResponse post = _HttpKit.post("http://xwqy.gsxt.gov.cn/mirco/micro_lib", param, "UTF-8");
	}

	public static void qichacha(String key) throws IOException {

		Connection con = Jsoup.connect("https://www.qichacha.com/search");
		con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/64.0");
		con.data("key", "测试企业");
		con.cookie("_uab_collina", "154564659656190500408653");
		con.cookie("acw_tc", "ca6cfacb15456465956333122e64628bee978d1f7ffc2d29e2fe069d76");
		con.cookie("hasShow", "1");
		con.cookie("QCCSESSID", "uq0aq1ricna82bc44i2fbrbn13");
		con.cookie("saveFpTip", "true");
		con.cookie("zg_de1d1a35bfa24ce29bbf2c7eb17e6c4f",
				"\"sid\": 1545646602996,\"updated\": 1545646812973,\"info\": 1545646603001,\"superProperty\": \"{}\",\"platform\": \"{}\",\"utm\": \"{}\",\"referrerDomain\": \"\",\"cuid\": \"74e0dcb73189a8746c5888af82c690e5\"}");
		con.cookie("zg_did", "{\"did\": \"167dfb77aef430-0672a71883b2a08-4c312e7e-100200-167dfb77af03d2\"}");

		Document document = con.get();
		Element elementById = document.getElementById("searchlist");

		if (elementById == null) {
			System.out.println("没有查询到数据");
			return;
		}

		Elements a_array = elementById.getElementsByClass("ma_h1");

		for (Element element : a_array) {

			//System.out.println(element.attr("href"));

			System.out.println(element.text());

			Connection connect = Jsoup.connect("https://www.qichacha.com" + element.attr("href"));

			connect.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/64.0");

			Document doc2 = connect.get();

			//System.out.println(doc2.toString());

			Elements table = doc2.getElementsByClass("ntable");

			Element table2 = table.get(1);

			System.out.println(table2.child(0).child(2).child(1).text());
		}

	}

}
